import { BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM } from "../../base/EnumIndex";
import { Tools } from "../../common/Tools";
import { AudioManager1 } from "../../manager/AudioManager1";
import { EventManager } from "../../manager/EventManager";
import { UIManager } from "../../manager/UIManager";
import LevelBase from "../../LevelBase";
import LvItem from "../../LvItem";
import LevelPre_15 from "./LevelPre_15";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Level_15_194 extends cc.Component {

    level15: LevelPre_15 = null
    audioCount: number = 0;

    /** 第几层 */
    louceng = 1;

    start() {
        this.level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelPre_15);
        EventManager.ins.on(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finish, this);

    }

    onDisable() {
        EventManager.ins.off(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finish, this);
    }


    gN(NodeName: string) {
        if (!this.level15) {
            this.level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelPre_15);
        }
        if (!this.level15.getNodeForName(NodeName).getComponent(LvItem)) {
            this.level15.getNodeForName(NodeName).addComponent(LvItem);
        }
        return this.level15.getNodeForName(NodeName);
    }
    gNa(NodeName: string, active: boolean) {
        this.level15.getNodeForName(NodeName).active = active;
    }

    async finish(finishStr: any, lvItem: LvItem) {
        if (typeof finishStr !== "string") { return }

        this.level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelPre_15);

        if (finishStr === "reviveGame") {
            console.log("复活");
            AudioManager1.playBG("恐怖关卡背景音乐", 1, BUNDLE_TYPE_ENUM.LEVEL)
            this.gN("duqi").opacity = 0;
            this.gNa("一楼伪人", true);
            this.gNa("二楼伪人", true);
            this.gNa("三楼伪人", true);
            this.gNa("四楼伪人", true);
            this.gNa("长官", false);
            this.gNa("屋子变暗", false);
            this.gNa("xiaohei", false);
            this.gNa("突脸", false);
            switch (this.louceng) {
                case 1:
                    this.gNa("一楼门", true);
                    this.gNa("下滑眼罩", true);
                    this.gNa("双击缝合线", true);
                    this.level15.removeStates(["1", "2"]);
                    this.gN("yanzhaonan").getComponent(sp.Skeleton).setAnimation(0, "daiji", true);
                    this.scheduleOnce(() => {
                        this.gN("yanzhaonan").getComponent(sp.Skeleton).setAttachment("站立眼睛掉", "站立眼罩");
                        this.gN("yanzhaonan").getComponent(sp.Skeleton).findBone("站立敞开领口").active = false;
                        this.gN("yanzhaonan").getComponent(sp.Skeleton).findBone("中毒敞开领口").active = false;
                        this.gN("yanzhaonan").getComponent(sp.Skeleton).findBone("中毒眼罩").active = true;
                    });
                    break;
                case 2:
                    this.gNa("二楼门", true);
                    this.level15.removeStates(["3", "4", "5", "假发"]);
                    this.gNa("拖动假发", true);
                    this.gNa("双击嘴巴", true);
                    this.gNa("拖动面膜", true);
                    this.gNa("下滑披肩", true);

                    this.gN("laotai").getComponent(sp.Skeleton).setAnimation(0, "daiji", true);

                    this.scheduleOnce(() => {
                        this.gN("laotai").getComponent(sp.Skeleton).setAttachment("待机五官", "待机五官");
                        this.gN("laotai").getComponent(sp.Skeleton).findBone("小手1").active = false;
                        this.gN("laotai").getComponent(sp.Skeleton).findBone("小手2").active = false;
                        this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒小手1").active = false;
                        this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒小手2").active = false;
                        this.gN("laotai").getComponent(sp.Skeleton).findBone("披肩").active = true;
                        this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒披肩").active = true;
                        this.gN("laotai").getComponent(sp.Skeleton).findBone("面膜").active = true;
                        this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒嘴巴脸").active = true;
                        this.gN("laotai").getComponent(sp.Skeleton).findBone("白头发").active = true;
                        this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒白头发").active = true;
                    })
                    break;
                case 3:
                    this.gNa("三楼门", true);
                    this.gNa("卸妆水", true);
                    this.gNa("双击骨钉1", true);
                    this.gNa("双击骨钉2", true);
                    this.gNa("拖动创可贴", true);
                    this.level15.removeStates(["6", "7", "创可贴"]);

                    this.gN("pengke").getComponent(sp.Skeleton).setAnimation(0, "daiji", true);
                    this.gN("pengke").getComponent(sp.Skeleton).findBone("骨钉").active = true;
                    this.gN("pengke").getComponent(sp.Skeleton).findBone("中毒骨钉").active = true;
                    this.scheduleOnce(() => {
                        this.gN("pengke").getComponent(sp.Skeleton).setAttachment("化妆脸", "正常脸");
                        this.gN("pengke").getComponent(sp.Skeleton).setAttachment("尖耳朵", "正常耳朵");
                        this.gN("pengke").getComponent(sp.Skeleton).setAttachment("伤口", "创可贴");

                        this.gN("pengke").getComponent(sp.Skeleton).setAttachment("中毒伤口", "中毒创可贴");
                        this.gN("pengke").getComponent(sp.Skeleton).setAttachment("中毒尖耳朵", "中毒正常耳朵");
                        this.gN("pengke").getComponent(sp.Skeleton).setAttachment("中毒不化妆脸", "正常脸中毒");
                    })
                    break;
                case 4:
                    this.gN("nng").getComponent(sp.Skeleton).setAnimation(0, "guiyi", true);
                    this.gNa("四楼门", true);
                    this.gNa("浴室门", true);
                    this.gNa("下滑伪人衣服", true);
                    this.gNa("毛巾", true);
                    this.gNa("niunaiqingjie", true);
                    this.gNa("nng", false);
                    this.gNa("右滑浴室门", false);
                    this.gNa("niunaigong", false);
                    this.gNa("toukan", true); 
                    this.gNa("jiancha", true)
                    this.gNa("nvjiancha", false)
                    Tools.setNodeParent(this.gN("niunaiqingjie"), this.gN("四楼伪人"))
                    this.level15.removeStates(["8", "9", "10"]);
                    break;
            }
            this.level15.runNodeTween(this.node, this.level15.getInterTwForName("fuhuo").interTween);

        }
        if (finishStr === "ifPen") {
            if (this.louceng == 4 && this.level15.isHaveNoStates(["8"])) {
                this.level15.runNodeTween(this.node, this.level15.getInterTwForName("直接抓捕").interTween);
            }else{
                this.level15.runNodeTween(this.node, this.level15.getInterTwForName("喷毒气").interTween);
            }
        }
        if (finishStr === "updateGame") {
            this.gNa("按钮", false);
            switch (this.louceng) {
                case 1:
                    this.gNa("一楼门", false);
                    AudioManager1.playAudio("男中毒", 1, BUNDLE_TYPE_ENUM.LEVEL)
                    if (this.level15.isHaveStates(["1", "2"])) {
                        this.gN("yanzhaonan").getComponent(sp.Skeleton).setAnimation(0, "dou", true);
                        this.gN("yanzhaonan").getComponent(sp.Skeleton).findBone("中毒敞开领口").active = true;
                        this.gN("yanzhaonan").getComponent(sp.Skeleton).findBone("中毒眼罩").active = false;
                        this.level15.addHaveStates(["11"])
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("下一层").interTween);
                        this.louceng++;
                    } else {
                        this.gN("yanzhaonan").getComponent(sp.Skeleton).setAnimation(0, "dou", true);
                        if (this.level15.isHaveStates(["1"])) {
                            this.gN("yanzhaonan").getComponent(sp.Skeleton).findBone("中毒眼罩").active = false;
                        }
                        if (this.level15.isHaveStates(["2"])) {
                            this.gN("yanzhaonan").getComponent(sp.Skeleton).findBone("中毒敞开领口").active = true;
                        }
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni2").interTween);
                    }
                    break;
                case 2:
                    this.gNa("二楼门", false);
                    AudioManager1.playAudio("女中毒", 1, BUNDLE_TYPE_ENUM.LEVEL)
                    this.gN("laotai").getComponent(sp.Skeleton).setAnimation(0, "du", true);
                    if (this.level15.isHaveStates(["3", "4", "5"])) {
                        this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒嘴巴脸").active = false;
                        this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒披肩").active = false;
                        this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒小手1").active = true;
                        this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒小手2").active = true;
                        if (this.level15.isHaveStates(["假发"])) {
                            this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒白头发").active = false;
                        }
                        this.scheduleOnce(() => {
                            this.gN("laotai").getComponent(sp.Skeleton).setAttachment("中毒中毒五官", "中毒嘴巴脸");
                        })
                        this.level15.addHaveStates(["12"])
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("下一层").interTween);
                        this.louceng++;
                        this.level15.removeStates(["假发"]);
                    } else {
                        if (this.level15.isHaveStates(["假发"])) {
                            this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒白头发").active = false;
                        }
                        if (this.level15.isHaveStates(["3"])) {
                            this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒嘴巴脸").active = false;
                        }
                        if (this.level15.isHaveStates(["4"])) {
                            this.scheduleOnce(() => {
                                this.gN("laotai").getComponent(sp.Skeleton).setAttachment("中毒中毒五官", "中毒嘴巴脸");
                            })
                        }
                        if (this.level15.isHaveStates(["5"])) {
                            this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒披肩").active = false;
                            this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒小手1").active = true;
                            this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒小手2").active = true;
                        }
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni2").interTween);
                    }
                    break;
                case 3:
                    this.gNa("三楼门", false);
                    AudioManager1.playAudio("女中毒", 1, BUNDLE_TYPE_ENUM.LEVEL)
                    this.gN("pengke").getComponent(sp.Skeleton).setAnimation(0, "du", true);
                    if (this.level15.isHaveStates(["6", "7"])) {
                        this.gN("pengke").getComponent(sp.Skeleton).findBone("中毒骨钉").active = false;
                        if (this.level15.isHaveStates(["创可贴"])) {
                            this.scheduleOnce(() => {
                                this.gN("pengke").getComponent(sp.Skeleton).setAttachment("中毒伤口", "中毒伤口");
                            })
                        }
                        this.scheduleOnce(() => {
                            this.gN("pengke").getComponent(sp.Skeleton).setAttachment("中毒不化妆脸", "中毒不化妆脸");
                            this.gN("pengke").getComponent(sp.Skeleton).setAttachment("中毒尖耳朵", "中毒尖耳朵");
                        })
                        this.level15.removeStates(["创可贴"]);
                        this.level15.addHaveStates(["13"])
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("下一层").interTween);
                        this.louceng++;
                    } else {
                        if (this.level15.isHaveStates(["创可贴"])) {
                            this.scheduleOnce(() => {
                                this.gN("pengke").getComponent(sp.Skeleton).setAttachment("中毒伤口", "中毒伤口");
                            })
                        }
                        if (this.level15.isHaveStates(["6"])) {
                            this.scheduleOnce(() => {
                            this.gN("pengke").getComponent(sp.Skeleton).setAttachment("中毒不化妆脸", "中毒不化妆脸");
                            })
                        }
                        if (this.level15.isHaveStates(["7"])) {
                            this.gN("pengke").getComponent(sp.Skeleton).findBone("中毒骨钉").active = false;
                            this.scheduleOnce(() => {
                                this.gN("pengke").getComponent(sp.Skeleton).setAttachment("中毒尖耳朵", "中毒尖耳朵");
                            })
                        }
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni2").interTween);
                    }
                    break;
                case 4:
                    this.gNa("四楼门", false);
                    if (this.level15.isHaveStates(["8", "9", "10"])) {
                        AudioManager1.playAudio("男中毒", 1, BUNDLE_TYPE_ENUM.LEVEL)
                        this.gN("toukan").getComponent(sp.Skeleton).setAnimation(0, "du", true);
                        this.level15.addHaveStates(["14"])
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("winGameAni").interTween);
                    } else {
                        if (this.level15.isHaveNoStates(["8"])) {
                            this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni4").interTween);
                        } else {
                            AudioManager1.playAudio("男中毒", 1, BUNDLE_TYPE_ENUM.LEVEL)
                            if (this.level15.isHaveNoStates(["9"])) {
                                this.gN("nng").getComponent(sp.Skeleton).setAnimation(0, "zhongdu_2", true);
                            } else {
                                this.gN("nng").getComponent(sp.Skeleton).setAnimation(0, "zhongdu_1", true);
                            }
                            this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni2").interTween);
                        }
                    }
                    break;
            }
        }
        if (finishStr === "updateGame2") {
            this.gNa("按钮", false);
            this.gN("jiancha").scaleX = -1;
            switch (this.louceng) {
                case 1:
                case 2:
                case 3:
                    let sayInfo1 = { "node": "行动队SayNode", "str": "该居民嫌疑已排除！", "audio": { "resUrl": "该居民嫌疑已排除！", "value": 1 } }
                    await this.level15.sayNodeQipaoTween(sayInfo1);
                    this.scheduleOnce(() => {
                        this.gNa("屋子变暗", true);
                        this.gNa("xiaohei", true);
                        this.gNa("一楼伪人", false);
                        this.gNa("二楼伪人", false);
                        this.gNa("三楼伪人", false);
                        cc.tween(this.gN("xiaohei")).set({ position: cc.v3(170, -550) }).delay(1).by(0.5, { x: -100 }).start();
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni1").interTween);
                    }, 2)
                    break;
                case 4:
                    if (this.level15.isHaveNoStates(["8"])) {
                        this.gNa("jiancha", false)
                        this.gNa("nvjiancha", true)
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni3").interTween);
                    } else {
                        let sayInfo1 = { "node": "行动队SayNode", "str": "该居民嫌疑已排除！", "audio": { "resUrl": "该居民嫌疑已排除！", "value": 1 } }
                        await this.level15.sayNodeQipaoTween(sayInfo1); this.scheduleOnce(() => {
                            this.gNa("屋子变暗", true);
                            this.gNa("toukan", false);
                            this.gNa("四楼伪人", false);
                            this.gNa("niunaigong", true);
                            cc.tween(this.gN("niunaigong")).set({ position: cc.v3(210, -570) }).delay(1).by(0.5, { x: -100 }).call(()=>{
                                this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni").interTween);
                            }).start();
                        }, 2)
                    }
                    break;
            }
        }
        if (finishStr === "突击检查") {
            switch (this.louceng) {
                case 1:
                    this.gNa("一楼门", false);
                    break;
                case 2:
                    this.gNa("二楼门", false);
                    break;
                case 3:
                    this.gNa("三楼门", false);
                    break;
                case 4:
                    this.gNa("四楼门", false);
                    break;
            }
            this.scheduleOnce(() => {
                this.gN("按钮").active = true;
                this.gN("按钮").opacity = 0;
                cc.tween(this.gN("按钮"))
                    .to(0.5, { opacity: 255 })
                    .call(() => {
                        UIManager.ins.enabledGameClick(true);
                    })
                    .start()
            }, 2.2)
        }
        if (finishStr === "startGame") {
            this.gN("yanzhaonan").getComponent(sp.Skeleton).setAttachment("站立眼睛掉", "站立眼罩");
            this.gN("yanzhaonan").getComponent(sp.Skeleton).findBone("站立敞开领口").active = false;
            this.gN("yanzhaonan").getComponent(sp.Skeleton).findBone("中毒敞开领口").active = false;
            this.gN("yanzhaonan").getComponent(sp.Skeleton).findBone("中毒眼罩").active = true;


            this.gN("laotai").getComponent(sp.Skeleton).findBone("小手1").active = false;
            this.gN("laotai").getComponent(sp.Skeleton).findBone("小手2").active = false;
            this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒小手1").active = false;
            this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒小手2").active = false;
            this.gN("laotai").getComponent(sp.Skeleton).findBone("披肩").active = true;
            this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒披肩").active = true;
            this.gN("laotai").getComponent(sp.Skeleton).findBone("中毒嘴巴脸").active = true;


            this.gN("pengke").getComponent(sp.Skeleton).findBone("骨钉").active = true;
            this.gN("pengke").getComponent(sp.Skeleton).setAttachment("化妆脸", "正常脸");
            this.gN("pengke").getComponent(sp.Skeleton).setAttachment("尖耳朵", "正常耳朵");
            this.gN("pengke").getComponent(sp.Skeleton).setAttachment("伤口", "创可贴");
            this.gN("pengke").getComponent(sp.Skeleton).setAttachment("中毒不化妆脸", "正常脸中毒");
        }
        if (finishStr.includes("提示字")) {
            this.typewriterEffect();
        }

        if (finishStr.includes("老太说话_")) {
            let time = finishStr.split("老太说话_")[1];
            if (this.level15.isHaveNoStates(["4"])) {
                this.gN("laotai").getComponent(sp.Skeleton).setAnimation(0, "sh", true);
                this.scheduleOnce(() => {
                    this.gN("laotai").getComponent(sp.Skeleton).setAnimation(0, "daiji", true);
                }, Number(time))
            }
        }
    }


    typewriterEffect() {
        this.schedule(this.typeNextCharacter, 1 / this.typingSpeed);
    }

    typeNextCharacter() {
        if (this.typingFinish) {
            this.unschedule(this.typeNextCharacter);
            return;
        }

        // 添加下一个字符到文本内容
        this.textLabel.getComponent(cc.Label).string += this.content[this.currentIndex];

        // 更新当前索引
        this.currentIndex++;

        // 判断是否打字完成
        if (this.currentIndex >= this.content.length) {
            this.typingFinish = true;
        }
    }

    @property(cc.Node)
    textLabel: cc.Node = null;

    @property({ displayName: "打字速度" })
    typingSpeed: number = 3; // 打字速度，每秒多少个字符

    @property({ displayName: "文本内容" })
    content: string = ""; // 完整的文本内容

    private currentIndex: number = 0; // 当前打字到的索引
    private typingFinish: boolean = false; // 打字是否完成



}
